/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.WechatScene;
import com.boxin.ims.modules.wechat.dao.WechatSceneDao;

/**
 * 微信带参数的二维码Service
 * @author xiaoqian
 * @version 2014-03-18
 */
@Component
@Transactional(readOnly = true)
public class WechatSceneService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(WechatSceneService.class);
	
	@Autowired
	private WechatSceneDao wechatSceneDao;
	
	public WechatScene get(Long id) {
		return wechatSceneDao.findOne(id);
	}
	
	public Page<WechatScene> find(Page<WechatScene> page, WechatScene wechatScene) {
		DetachedCriteria dc = wechatSceneDao.createDetachedCriteria();
		if (wechatScene.getUser()!=null && wechatScene.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", wechatScene.getUser().getId()));
		}
		if (wechatScene.getScene()!=null ){
			dc.add(Restrictions.eq("scene", wechatScene.getScene()));
		}
		dc.addOrder(Order.desc("id"));
		return wechatSceneDao.find(page, dc);
	}
	
	public List<WechatScene> find(WechatScene wechatScene){
		DetachedCriteria dc = wechatSceneDao.createDetachedCriteria();
		if (wechatScene.getUser()!=null && wechatScene.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", wechatScene.getUser().getId()));
		}
		
		return wechatSceneDao.find(dc);
	}
	public WechatScene get(WechatScene wechatScene){
		DetachedCriteria dc = wechatSceneDao.createDetachedCriteria();
		if (wechatScene.getUser()!=null && wechatScene.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", wechatScene.getUser().getId()));
		}
		if(wechatScene.getSceneId()!=null){
			dc.add(Restrictions.eq("sceneId", wechatScene.getSceneId()));
		}
		 List<WechatScene> list=wechatSceneDao.find(dc);
		return list!=null&&list.size()>0?list.get(0):null;
	}
	
	public WechatScene wechatSceneBySceneId(Long user_id,String sceneId ){
		return wechatSceneDao.wechatSceneBySceneId(user_id, sceneId);
	}
	
	public Long getSceneIdByUserId(User user){
		Long sceneId= wechatSceneDao.getSceneId(user.getId());
		if(sceneId==null){
			return 0L;
		}else{
			return sceneId+1;
		}
	}
	
	public WechatScene wechatSceneByTicket(Long user_id,String sceneId, String ticket){
		return wechatSceneDao.wechatSceneByTicket(user_id, sceneId, ticket);
	}
	
	public WechatScene wechatSceneBySceneId(Long user_id,Long sceneId ){
		return wechatSceneDao.wechatSceneBySceneId(user_id, sceneId);
	}
	
	public Long getSceneId(){
		User user=UserUtils.getUser();
		Long sceneId= wechatSceneDao.getSceneId(4l);
		if(sceneId==null){
			return 0L;
		}else{
			return sceneId+1;
		}
	}
	public Long getSceneIdStr(){
		User user=UserUtils.getUser();
		Long sceneId= wechatSceneDao.getSceneIdStr(user.getId());
		if(sceneId==null){
			return 0L;
		}else{
			return sceneId+1;
		}
	}
	
	
	public Long getSceneIdByUserId(Long userId){
		Long sceneId= wechatSceneDao.getSceneId(userId);
		if(sceneId==null){
			return 0L;
		}else{
			return sceneId+1;
		}
	}
	
	
	@Transactional(readOnly = false)
	public void save(WechatScene wechatScene) {
		wechatSceneDao.save(wechatScene);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		wechatSceneDao.deleteById(id);
	}
	
}
